package com.hghivln.controller.system;

import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.pojo.dto.RoleDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.RoleService;
import com.hghivln.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     */
    @GetMapping("listRoleForPage")
    public AjaxResult listRoleForPage(RoleDto roleDto) {
        return roleService.listRoleForPage(roleDto);
    }

    /**
     * 添加
     */
    @PostMapping("addRole")
    @Log(title = "添加角色", businessType = BusinessType.INSERT)
    public AjaxResult addRole(@Validated RoleDto roleDto) {
        roleDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return roleService.addRole(roleDto);
    }

    /**
     * 修改
     */
    @PutMapping("updateRole")
    @Log(title = "修改角色", businessType = BusinessType.UPDATE)
    public AjaxResult updateRole(@Validated RoleDto roleDto) {
        roleDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return roleService.updateRole(roleDto);
    }

    /**
     * 删除
     */
    @DeleteMapping("deleteRoleByIds/{ids}")
    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    public AjaxResult deleteRoleByIds(@PathVariable @Validated @NotEmpty(message = "要删除的角色ID不能为空") Long[] ids) {
        return roleService.deleteRoleByIds(ids);
    }

    /**
     * 根据ID查询一个角色信息
     */
    @GetMapping("getRoleById/{id}")
    public AjaxResult getRoleById(@PathVariable @Validated @NotNull(message = "角色ID不能为空") Long id) {
        return roleService.getRoleById(id);
    }

    /**
     * 保存角色和菜单权限之间的关系
     */
    @PutMapping("saveRoleMenu/{roleId}/{menuIds}")
    public AjaxResult saveRoleMenu(Long roleId, @PathVariable @Validated @NotEmpty(message = "菜单ID不能为空") Long[] menuIds) {
        // 因为我们用的路径参数，前端可能传过来的menuIds是一个空的，但是为空的话无法匹配上面的路径
        // 所以如果为空，我们让前端传一个-1过来，如果是-1说明当前角色一个权限也没有选择
        if (menuIds.length == 1 && menuIds[0].equals(-1L)) {
            menuIds = new Long[]{};
        }
        roleService.saveRoleMenu(roleId, menuIds);
        return AjaxResult.success();
    }

    /**
     * 查询所有可用角色
     */
    @GetMapping("selectAllRole")
    public AjaxResult selectAllRole() {
        return roleService.selectAllRole();
    }

    /**
     * 根据用户ID查询用户拥有的角色IDS
     */
    @GetMapping("getRoleIdsByUserId/{userId}")
    public AjaxResult getRoleIdsByUserId(@PathVariable @Validated @NotNull(message = "用户ID不能为空") Long userId) {
        return roleService.selectRoleIdsByUserId(userId);
    }

    /**
     * 保存角色和用户之间的关系
     */
    @PutMapping("saveRoleUser/{userId}/{roleId}")
    public AjaxResult saveRoleUser(@PathVariable("userId") Long userId, @PathVariable("roleId") @Validated @NotEmpty(message = "角色ID不能为空") Long[] roleId) {
        if (roleId.length == 1 && roleId[0].equals(-1L)){
            roleId = new Long[] {};
        }
        roleService.saveRoleUser(userId,roleId);
        return AjaxResult.success();
    }
}
